//
//  YREmotionKeyBoard.swift
//  新浪微博
//
//  Created by Viper on 16/7/4.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

class YREmotionKeyBoard: UIView {


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
            addSubview(emotionToolbar)
            addSubview(pageView)
        addSubview(pageControl)
        
        emotionToolbar.snp_makeConstraints { (make) in
            
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        pageView.snp_makeConstraints { (make) in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emotionToolbar.snp_top)
            
        }
        pageControl.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(pageView)
            make.centerX.equalTo(pageView)
            
        }
        
        
        //实现闭包
        emotionToolbar.toolbarClosure = {[weak self](type : YREmotionToolbarType)->() in
            //获取indexPath
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //UICollectionViewScrollPosition->对齐方式
            //让配图滚动到指定位置
        self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            //总页数
            self?.pageControl.numberOfPages = YREmotionTools.sharedTools.allEmotions[indexPath.section].count
            //当前页数
            self?.pageControl.currentPage = 0
            
            
        }
    }
    //MARK: - 懒加载控件
    
    private lazy var emotionToolbar : YREmotionToolbar = YREmotionToolbar()
    
     //表情键盘
    private lazy var pageView : YREmotionPageView = {
        
        let view = YREmotionPageView()
        //设置代理
        view.delegate = self
        //关闭弹簧效果
        view.bounces = false
        //分页
        view.pagingEnabled = true
        //隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        return view
        
    }()
    
    //pageControl
    private lazy var pageControl : UIPageControl = {
        
       let view = UIPageControl()
        //总页数
        view.numberOfPages = YREmotionTools.sharedTools.allEmotions[0].count
        //当前页数
        view.currentPage = 0
        
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //选中颜色 - >通过kvc赋值 更清晰
        view.setValue( UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue( UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
        
    }()

}
extension YREmotionKeyBoard : UICollectionViewDelegate{
    
    /*
     -计算方式->（只试用于cell和pageView一样大）
     -当前的pageView界面上展示给用户最多2个cell
     -相对于pageView左侧起点为0点而屏幕中心点 X = 屏幕的宽度 / 2 + contentOffset.x
     -由于cell贴在pageView每个cell都有自己的frame 
     -如果判断出哪个cell的frame包含了屏幕的中心点 就代表现实的多
     -通过cell 和 pageView 得到 indexPath
     -通过indexPath得到section
     -通过section =  0 ， 1 ， 2  viewWithTag 通过该方法得到button 把button状态改变
     */
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
//        print(scrollView.contentOffset.x)
        
        //获取屏幕的中心点
        let centerX = SCREENW / 2 + scrollView.contentOffset.x
        
        //获取当前屏幕显示的cell  重新排序 .sort.....
        let cellArray = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
            
        }
        
        //如果屏幕上显示两个cell才判断
        if cellArray.count == 2 {
            
//            print(cellArray)
            
            //第一个cell
            let firstCell = cellArray.first!
            
            //第二个cell
            let lastCell = cellArray.last!
            //获取cell显示多得cell的indexPath
            var indexPath : NSIndexPath
            
        //判断中心点是否在第一cell中  如果在 代表第一个cell显示的多
             if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
            
//            print("第一个cell")
                
               indexPath = pageView.indexPathForCell(firstCell)!
            
            }else{
            
//             print("第二个cell")
                
               indexPath = pageView.indexPathForCell(lastCell)!
            
            }
            
//            print(indexPath.section)
            //滚动pageView改变toolbar下面按钮的状态
            emotionToolbar.setCurrentButtons(indexPath.section)
            //总页数
            pageControl.numberOfPages = YREmotionTools.sharedTools.allEmotions[indexPath.section].count
            //当前页数
            pageControl.currentPage = indexPath.item
            
            
        }
        
        
    }
    
    
}

